<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #app {
            margin: 0 auto;
            width: 300px;
            border: 1px solid aqua;
        }
        
        .title {
            width: 300px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            background-color: aqua;
        }
        
        .item {
            height: 50px;
            position: relative;
            font-size: 10px;
            display: flex;
            border-bottom: 1px solid aqua;
        }
        
        a {
            display: inline-block;
            width: 20px;
            height: 20px;
            background-color: rgb(212, 211, 211);
            text-decoration: none;
            text-align: center;
            line-height: 20px;
        }
        
        .item>img {
            position: absolute;
            margin: 10px;
            width: 50px;
            height: 30px;
        }
        
        .item .change {
            position: absolute;
            width: 100px;
            left: 170px;
            top: 15px;
        }
        
        .item input {
            width: 30px;
            height: 17px;
        }
        
        .item .name {
            position: absolute;
            top: 20px;
            left: 70px;
        }
        
        .item .del {
            position: absolute;
            font-size: 25px;
            color: red;
            top: 7px;
            left: 270px;
            cursor: pointer;
        }
        
        .total {
            position: relative;
            height: 49px;
            background-color: aquamarine;
        }
        
        .total span {
            position: absolute;
            display: inline-block;
            height: 49px;
            text-align: center;
            line-height: 49px;
            left: 80px;
        }
        
        .total button {
            position: absolute;
            background-color: rgb(240, 75, 108);
            left: 200px;
            top: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container">
            <my-cart></my-cart>
        </div>
    </div>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript">
        // 组件内容
        var CartTitle = {
            props: ['uname'],
            template: `<div class="title">{{uname}}的商品</div>`
        }
        var CartList = {
            props: ['list'],
            template: `
                <div>
                    <div :key='item.id' v-for='item in list' class="item">
                        <img :src="item.img" />
                        <div class="name">{{item.name}}</div>
                        <div class="change">
                            <a href="" @click.prevent='sub(item.id)'>-</a>
                            <input type="text" class="num" :value='item.num' @blur='changeNum(item.id,$event)'/>
                            <a href="" @click.prevent='add(item.id)'>+</a>
                        </div>
                        <div class="del" @click='del(item.id)'>×</div>
                    </div>
                </div>
             `,
            methods: {
                changeNum: function(id, event) {
                    //  拿到当前最新的值
                    // console.log(id,event.target.value);
                    this.$emit('change-num', {
                        id: id,
                        type: 'change',
                        num: event.target.value
                    })
                },
                sub: function(id) {
                    this.$emit('change-num', {
                        id: id,
                        type: 'sub',
                    });
                },
                add: function(id) {
                    this.$emit('change-num', {
                        id: id,
                        type: 'add',
                    });
                },
                del: function(id) {
                    //  通过自定义自建的方式 把id传递给父组件
                    this.$emit('cart-del', id);
                }
            }
        }
        var CartTotal = {
            props: ['list'],
            template: `
            <div class="total">
                <span>总价：{{total}}</span>
                <button>结算</button>
             </div>
            `,
            // 计算属性
            computed: {
                total: function() {
                    // 计算商品总价
                    var t = 0;
                    this.list.forEach(item => {
                        t += item.price * item.num;
                    });
                    return t;
                }
            }
        }

        // 全局组件化 父组件
        Vue.component('my-cart', {
            data: function() {
                return {
                    uname: 'wd',
                    list: [{
                        id: 1,
                        name: 'TCL彩电',
                        price: 1000,
                        num: 1,
                        img: './img/01.jpeg'
                    }, {
                        id: 2,
                        name: 'TCL彩电',
                        price: 1000,
                        num: 1,
                        img: './img/02.png'
                    }, {
                        id: 3,
                        name: 'TCL彩电',
                        price: 1000,
                        num: 1,
                        img: './img/03.jpeg'
                    }, {
                        id: 4,
                        name: 'TCL彩电',
                        price: 1000,
                        num: 1,
                        img: './img/04.png'
                    }]
                }
            },
            // 模板信息
            template: `
            <div class="cart">
                <cart-title :uname='uname'></cart-title>
                <cart-list :list='list' @change-num='changeNum($event)' @cart-del='delCart($event)'></cart-list>
                <cart-total :list='list'></cart-total>
            </div>
            `,
            // 子组件
            components: {
                'cart-title': CartTitle,
                'cart-list': CartList,
                'cart-total': CartTotal,
            },
            methods: {
                changeNum: function(val) {
                    // 分为三种情况：输入域变更，加号变更，减号变更
                    if (val.type == 'change') {
                        // 根据子组件传递过来的数据，更新list中对应的数据
                        this.list.some(item => {
                            if (item.id == val.id) {
                                item.num = val.num;
                                // 终止遍历
                                return true;
                            }
                        });
                    } else if (val.type == 'sub') {
                        // 减1操作
                        this.list.some(item => {
                            if (item.id == val.id) {
                                item.num -= 1;
                                // 终止遍历
                                item.num = 0;
                                return true;
                            }
                        })
                    } else if (val.type == 'add') {
                        // 加1操作
                        this.list.some(item => {
                            if (item.id == val.id) {
                                item.num += 1;
                                // 终止遍历
                                return true;
                            }
                        });
                    }
                },
                delCart: function(id) {
                    // 根据id删除list中对应的数据
                    // 找到id对应数据索引
                    var index = this.list.findIndex(item => {
                        return item.id == id;
                    });
                    // 根据索引删除对应数据
                    this.list.splice(index, 1);
                }
            }
        })

        var m = new Vue({
            el: '#app',
            data: {

            }
        })
    </script>
</body>

</html>